package com.bawei.home.model

import com.bawei.common.entity.CommendEntity
import com.bawei.common.entity.SimpleVideoEntity
import com.bawei.common.model.BaseRepository
import com.bawei.home.SimpleTypeEntity
import com.bawei.home.api.HomeServerApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

/**
 * Author: Jason Zhang
 * Date: 2025/1/16
 * Desc: Home数据仓库
 */
class HomeRepository @Inject constructor(private val apiServer: HomeServerApi): BaseRepository<HomeServerApi>(){


    suspend fun getSimpleTypes(): Flow<List<SimpleTypeEntity>>{
        return flow {
            var result = apiServer.getSimpleTypes()
            if (result.code == 0){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }

    suspend fun getSimpleVideoByChannelId(channelId: String, page: Int, pageSize: Int): Flow<List<SimpleVideoEntity>> {
        return flow {
            var result = apiServer.getSimpleVideoByChannelId(channelId, page, pageSize)
            if (result.code == 0){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }

    suspend fun getRecommendVideo(page: Int, pageSize: Int): Flow<List<SimpleVideoEntity>>{
        return flow {
            var result = apiServer.getRecommendSimpleVideo(page, pageSize)
            if (result.code == 0){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }

    suspend fun getSimpleVideoCommends(dataType: Int, itemId: String): Flow<List<CommendEntity>>{
        return flow {
            var result =  apiServer.getVideoCommends(dataType, itemId)
            if (result.code == 0){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }

}